<script setup>
import { ref,reactive,onBeforeMount } from 'vue'
import useBannersStore from '../../store/bannersStore'
import Dialog from './dialog.vue'

const bannersStore = useBannersStore()

// Dialog组件上的ref
const dialog = ref(null)

onBeforeMount(() => {
  // 获取轮播图数据
  bannersStore.getBannersAction()
})

// 新增
function addBanner() {
  // 打开对话框
  bannersStore.openDialog()
}
// 编辑
function editBanner(banner) {
  // console.log(banner,'编辑')
  dialog.value.editId = banner.id
  dialog.value.bannerForm.title = banner.title
  dialog.value.bannerForm.img = banner.img
  dialog.value.bannerForm.status = banner.status
  dialog.value.fileList.list.push({
    url: "http://localhost:3000" + banner.img
  });
  // 打开对话框
  bannersStore.openDialog()
}
// 删除
function confirmEvent(id) {
  // console.log(id,'删除')
  bannersStore.delBannersAction(id)
}
</script>

<template>
  <el-card>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统设置</el-breadcrumb-item>
      <el-breadcrumb-item>轮播图管理</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="right">
      <el-button @click="addBanner" type="primary">新增轮播图</el-button>
    </div>
    <!-- 表格 -->
    <el-table :data="bannersStore.bannersList" row-key="id" style="100%" max-height="480">
      <el-table-column prop="id" label="序号" width="80" fixed> </el-table-column>
      <el-table-column prop="title" label="图片名称" width="150">
      </el-table-column>
      <el-table-column prop="img" label="轮播图片">
        <template v-slot="props">
          <el-avatar
            v-if="props.row.pid !== 0"
            shape="square"
            :size="100"
            fit="cover"
            :src="'http://localhost:3000' + props.row.img"
          ></el-avatar>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="150">
        <template v-slot="props">
          <el-tag type="success" size="small" v-if="props.row.status === 1">启用</el-tag>
          <el-tag type="danger" size="small" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" min-width="150">
        <template v-slot="props">
          <el-button type="primary" size="small" round @click="editBanner(props.row)">编辑</el-button>
          <!-- 气泡确认框 -->
          <el-popconfirm
            width="160px"
            title="你确定要删除吗？"
            @confirm="confirmEvent(props.row.id)"
          >
            <template #reference>
              <el-button type="danger" size="small" round>删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <!-- 对话框 -->
  <Dialog ref="dialog" />
</template>

<style lang="scss" scoped>

</style>
